package com.hjl.sr.security.token;

import com.hjl.sr.remoting.EncryptContext;
import com.hjl.sr.security.SecurityUtil;
import com.hjl.sr.security.token.store.TokenStoreService;

public class ServerTokenManager implements TokenManager {
	
	private TokenStoreService tokenStoreService;
	
	public ServerTokenManager(TokenStoreService tokenStoreService) {
		super();
		this.tokenStoreService = tokenStoreService;
	}

	public String getEncryptToken(EncryptContext encryptContext) {
		return null;
	}

	public String getDecryptToken(EncryptContext encryptContext) {
		String token =  encryptContext.getHeader(SecurityUtil.TOKEN_HEADER_NAME);
		if (!tokenStoreService.exist(token)) {
			throw new TokenNotMatchException("not exist!");
		}
		return token;
	}

	public void clearToken(EncryptContext encryptContext, String token) {
		tokenStoreService.delete(token);
	}

}
